<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-button type="primary" size="small" @click="addPerm(1,'0')">
            新增权限
          </el-button>
        </el-row>
        <el-table :data="permissionList" row-key="id" default-expand-all>
          <el-table-column align="center" label="权限名称" prop="name" />
          <el-table-column align="center" label="权限标识" prop="code" />
          <el-table-column align="center" label="权限描述" prop="description" />
          <el-table-column align="center" label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" type="text" @click="addPerm(2,row.id)">添加权限</el-button>
              <el-button type="text" @click="editPerm(row.id)">编辑权限</el-button>
              <el-button type="text" @click="delPerm(row.id)">删除权限</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="`${showText}权限点`" :visible="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 引入权限列表 , 删除权限, 编辑权限, 保存权限, 新增权限
import { getPermissionList, delPermission, getPermissionDetail, updatePermission, addPermission } from '@/api/permission'
// 引入树形 查找
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      // 权限列表
      permissionList: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        enVisible: '1' // 开启
      },
      showDialog: false,
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' },
          { max: 10, message: '名称最大10位', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' },
          { max: 20, message: '名称最大20位', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '保存'
    }
  },
  created() {
    // 页面进来加载
    this.getPermissionList()
  },
  methods: {
    // 权限列表
    async getPermissionList() {
      // 权限列表封装请求  树形查找
      this.permissionList = listToTreeData(await getPermissionList(), '0')
      // console.log(this.permissionList)
    },
    // 删除权限
    async delPerm(id) {
      // 二次询问
      await this.$confirm('确认要删除？')

      // 请求发送删除
      await delPermission(id)

      // 提示用户
      this.$message.success('删除成功')
      // 更新
      this.getPermissionList()
    },
    // 添加
    addPerm(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      // 弹窗
      this.showDialog = true
    },
    // 确认保存
    async btnOK() {
      // 校验  validate
      await this.$refs.perForm.validate()
      // 发送请求
      if (this.formData.id) {
        // 编辑
        await updatePermission(this.formData)
      } else {
        // 新增
        await addPermission(this.formData)
      }
      // 提示用户
      this.$message.success('添加成功')
      // 关闭弹窗
      this.showDialog = false
      // 更新
      this.getPermissionList()
    },
    // 编辑
    async editPerm(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 取消
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        enVisible: '1' // 开启
      }
      // 清理校验
      this.$refs.perForm.resetFields()
      // 关闭弹窗
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
